package org.csu.onlineedubackend.service.impl;

import org.csu.onlineedubackend.entity.Log;
import org.csu.onlineedubackend.mapper.LogMapper;
import org.csu.onlineedubackend.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.String;
import java.lang.Integer;
import java.lang.Float;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public void recordLogin(Integer userId, String username, String ip, String userAgent, boolean success, String message) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("SECURITY");
        log.setLevel(success ? "INFO" : "WARN");
        log.setModule("登录");
        log.setAction(success ? "LOGIN" : "LOGIN_FAIL");
        log.setMessage(message);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordLogout(Integer userId, String username, String ip, String userAgent, String message) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("SECURITY");
        log.setLevel("INFO");
        log.setModule("登录");
        log.setAction("LOGOUT");
        log.setMessage(message);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordCourseLike(Integer userId, String username, Integer courseId, String courseName, String ip, String userAgent, boolean isLike) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("OPERATION");
        log.setLevel("INFO");
        log.setModule("学生");
        log.setAction(isLike ? "LIKE" : "UNLIKE");
        log.setTargetId(courseId != null ? courseId.toString() : null);
        log.setTargetType("课程");
        log.setMessage((isLike ? "收藏" : "取消收藏") + "课程: " + courseName);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordCourseSubscribe(Integer userId, String username, Integer courseId, String courseTitle, String ip, String userAgent, boolean isSubscribe) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("OPERATION");
        log.setLevel("INFO");
        log.setModule("课程");
        log.setAction(isSubscribe ? "SUBSCRIBE" : "UNSUBSCRIBE");
        log.setTargetId(courseId != null ? courseId.toString() : null);
        log.setTargetType("课程");
        log.setMessage((isSubscribe ? "订阅" : "取消订阅") + "课程: " + courseTitle);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordCourseComment(Integer userId, String username, Integer courseId, String courseTitle, String content, Integer rating, String ip, String userAgent) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("OPERATION");
        log.setLevel("INFO");
        log.setModule("课程");
        log.setAction("COMMENT");
        log.setTargetId(courseId != null ? courseId.toString() : null);
        log.setTargetType("课程");
        log.setMessage("评价课程: " + courseTitle + ", 评分: " + rating + ", 内容: " + content);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordCourseProgress(Integer userId, String username, Integer courseId, String courseTitle, Float progress, String ip, String userAgent) {
        Log log = new Log();
        log.setUserId(userId);
        log.setUsername(username);
        log.setLogType("BEHAVIOR");
        log.setLevel("INFO");
        log.setModule("课程");
        log.setAction("PROGRESS_UPDATE");
        log.setTargetId(courseId != null ? courseId.toString() : null);
        log.setTargetType("课程");
        log.setMessage("更新课程进度: " + courseTitle + ", 进度: " + progress);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }

    @Override
    public void recordTeacherCourseOperation(Integer teacherId, String username, Integer courseId, String courseTitle, String action, String detail, String ip, String userAgent) {
        Log log = new Log();
        log.setUserId(teacherId);
        log.setUsername(username);
        log.setLogType("OPERATION");
        log.setLevel("INFO");
        log.setModule("课程操作");
        log.setAction(action);
        log.setTargetId(courseId != null ? courseId.toString() : null);
        log.setTargetType("课程");
        log.setMessage("教师操作: " + action + "，课程: " + courseTitle + "，详情: " + detail);
        log.setIpAddress(ip);
        log.setUserAgent(userAgent);
        logMapper.insert(log);
    }
}